package com.irdstudio.apicenter.console.startup;

import javax.servlet.ServletContextEvent;
import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.ContextLoaderListener;

import com.irdstudio.apicenter.frame.core.jdbc.container.PageInfo;
import com.irdstudio.apicenter.frame.core.jdbc.tablemodel.ITableModelDAO;
import com.irdstudio.apicenter.frame.core.jdbc.util.ConnectionManager;
import com.irdstudio.apicenter.frame.service.DataDicService;
import com.irdstudio.apicenter.frame.service.MacmsPathService;
import com.irdstudio.apicenter.frame.service.SvnProjectService;
import com.irdstudio.apicenter.frame.util.BeanUtils;

public class StartupListener extends ContextLoaderListener {

	@Override
	public void contextInitialized(ServletContextEvent event) {

		System.out
				.println("----------------------------------------------------------------");
		System.out.println("开始初始化Spring Bean容器...");

		super.contextInitialized(event);

		// ApplicationContext ac = WebApplicationContextUtils
		// .getRequiredWebApplicationContext(event.getServletContext());

		initializeMacmsEnv();
	}

	/**
	 * MACMS系统启动初始化
	 * 
	 * @param ac
	 */
	private void initializeMacmsEnv() {
		Logger logger = LoggerFactory.getLogger(StartupListener.class);
		logger
				.info("----------------------------------------------------------------");
		logger.info("开始初始化MACMS系统运行环境...");

		// 将数据源注入到连接管理类
		ConnectionManager.setDataSource(BeanUtils.getBean(DataSource.class));

		// 初始化系统路径服务组件
		MacmsRoot
				.setMacmsPathService(BeanUtils.getBean(MacmsPathService.class));

		// 初始化Svn工程服务组件
		MacmsRoot.setSvnProjectService(BeanUtils
				.getBean(SvnProjectService.class));

		// 初始化字典服务(数据字典项读入缓存)
		MacmsRoot.setDataDicService(BeanUtils.getBean(DataDicService.class));

		ITableModelDAO tableModelDao = (ITableModelDAO) BeanUtils
				.getBean("tableModelDAO");
		PageInfo pageInfo = new PageInfo();
		pageInfo.pageSize = 2;
		pageInfo.pageIdx = 2;
		System.out.println(tableModelDao.queryList(
				"select * from app_frame_module where app_id=?", pageInfo,
				new Object[] { "0002" }));

		System.out.println(pageInfo.recordSize);

		logger.info("初始化MACMS系统运行环境完成");
		logger
				.info("----------------------------------------------------------------");

	}

}
